package engine.objects;

import java.util.HashMap;
import java.util.Random;

import engine.game.Manager;
import engine.graphics.TextureManager;
import engine.graphics.Vertex2f;
import engine.system.ID;
import engine.system.IInit;

public abstract class ContainerObject extends PhysicsObject implements IInit {

	private static final long serialVersionUID = 1L;

	private static Random r = new Random();

	private HashMap<String, PhysicsObject> objects;
	private HashMap<String, String> textures;

	public ContainerObject(Manager m, int x, int y, int w, int h) {
		this(m, x, y, w, h, false, true);
	}

	public ContainerObject(Manager m, int x, int y, int w, int h,
			boolean isCollidable) {
		this(m, x, y, w, h, isCollidable, true);
	}

	public ContainerObject(Manager m, int x, int y, int w, int h,
			boolean collidable, boolean isStatic) {
		super(m, x, y, w, h, collidable, isStatic);
	}

	public HashMap<String, PhysicsObject> getObjects() {
		return objects;
	}

	private void initMaps() {
		this.objects = new HashMap<String, PhysicsObject>();
		this.textures = new HashMap<String, String>();
	}

	public void subdivide(int w, int h, String animName, String texName, ID[] id) {
		initMaps();
		int x = this.getObjectWidth() / w;
		int y = this.getObjectHeight() / h;
		if (w > this.getObjectWidth())
			w = this.getObjectWidth();
		if (h > this.getObjectHeight())
			h = this.getObjectHeight();
		for (int x1 = 0; x1 < x; x1++)
			for (int y1 = 0; y1 < y; y1++) {
				int l = (int) this.getMinX() + x1 * w;
				int t = (int) this.getMinY() + y1 * h;
				ID tmpID = id[r.nextInt(id.length)];
				PhysicsObject tmp = new PhysicsObject(this.getManager(), l, t,
						w, h, false, true);
				TexturedObject.addNewAnimationToObject(tmp, animName, texName,
						64, 64, new ID[] { tmpID }, 0);
				tmp.changeColor(this.getColor());
				String name = animName + "_" + x1 + "_" + y1;
				this.objects.put(name, tmp);
				this.textures.put(name, texName);
			}
	}

	public void subdivide(int w, int h, String animName, String texName,
			Vertex2f[] imgCoords) {
		initMaps();
		int x = this.getObjectWidth() / w;
		int y = this.getObjectHeight() / h;
		if (w > this.getObjectWidth())
			w = this.getObjectWidth();
		if (h > this.getObjectHeight())
			h = this.getObjectHeight();
		int id = 0 ; 
		for (int x1 = 0; x1 < x; x1++) {
			for (int y1 = 0; y1 < y; y1++) {
				int l = (int) this.getMinX() + x1 * w;
				int t = (int) this.getMinY() + y1 * h;
				id = r.nextInt(imgCoords.length); 
				Vertex2f tmpCoord = imgCoords[id];
				PhysicsObject tmp = new PhysicsObject(this.getManager(), l, t,
						w, h, false, true);
				TexturedObject.addNewAnimationToObject(tmp, animName, texName,
						new Vertex2f[] { new Vertex2f(w, h) },
						new Vertex2f[] { tmpCoord }, 0);
				tmp.changeColor(this.getColor());
				String name = animName + "_" + x1 + "_" + y1;
				this.objects.put(name, tmp);
				this.textures.put(name, texName);
			}
		}
	}

	public void subdivide(int w, int h, String animName, String texName,
			ID[] id, int delay) {
		initMaps();
		int x = this.getObjectWidth() / w;
		int y = this.getObjectHeight() / h;
		if (w > this.getObjectWidth())
			w = this.getObjectWidth();
		if (h > this.getObjectHeight())
			h = this.getObjectHeight();
		for (int x1 = 0; x1 < x; x1++)
			for (int y1 = 0; y1 < y; y1++) {
				int l = (int) this.getMinX() + x1 * w;
				int t = (int) this.getMinY() + y1 * h;
				PhysicsObject tmp = new PhysicsObject(this.getManager(), l, t,
						w, h, false, true);
				TexturedObject.addNewAnimationToObject(tmp, animName, texName,
						64, 64, id, delay);
				tmp.changeColor(this.getColor());
				String name = animName + "_" + x1 + "_" + y1;
				this.objects.put(name, tmp);
				this.textures.put(name, texName);
			}
	}

	public void update() {
		super.update();
		if (!(this.objects == null))
			for (PhysicsObject o : objects.values()) {
				o.update();
			}
	}

	public void render() {
		if (!(this.objects == null)) {
			String lastTexName = "";
			for (String s : this.objects.keySet()) {
				if (!s.equals(lastTexName)) {
					lastTexName = s;
					TextureManager.bindTexture(textures.get(lastTexName));
				}
				this.objects.get(s).render();
			}
		} else
			super.render();
	}

}
